import PlatformModule from "./PlatformModule";
import Common from "../utils/Common";
import { MSG } from "../const/MSG";
import { Debug } from "../utils/Debug";

export default class BDModule extends PlatformModule {
    constructor() {
        super();
    }
    public platformName: string = "swan";
    public appSid: string = "";

    public recordRes: any = null;
    public recordCb: any = null;

    public _createBannerAd() {
        if (!this.checkWindow()) return;
        var win = this.getWindow();
        if (!win[this.platformName].createBannerAd) return;
        let wxsys = this.getSystemInfoSync();

        let windowWidth = wxsys.screenWidth;
        let windowHeight = wxsys.screenHeight;
        if (Common.isEmpty(this.getBannerId())) {
            Debug.warn(MSG.BANNER_KEY_IS_NULL)
            return;
        }
        let banner = win[this.platformName].createBannerAd({
            adUnitId: this.getBannerId(),
            appSid: this.appSid,
            style: {
                top: windowHeight,
                //lef: (750 - 300) / 2 / Laya.Browser.pixelRatio,
                width: windowWidth
            }
        });
        return banner;
    }

    public createRewardAD(show: boolean) {
        if (this.videoLoading) {
            return;
        }
        if (!this.checkWindow()) return;
        var win = this.getWindow();
        if (!win[this.platformName].createRewardedVideoAd) {
            return;
        }
        if (this.mVideo) {
            this.mVideo.offClose(this._onVideoClose);
            this.mVideo.offError(this._onVideoError);
            this.mVideo.offLoad(this._onVideoLoad);
        } else {
            this.mVideo = win[this.platformName].createRewardedVideoAd({
                adUnitId: this.getVideoId(),
                appSid: this.appSid
            });
        }
        this.mVideo.onError(this._onVideoError);
        this.mVideo.onClose(this._onVideoClose);
        this.mVideo.onLoad(this._onVideoLoad);
        this.videoLoading = true;
        this.mVideo.load()
            .then(() => {
                if (show) {
                    this.mVideo.show().then(() => { }).catch((err: any) => {
                        this._onVideoError(err.errMsg, err.errCode);
                        Debug.log(err.errMsg);
                    });
                }
            }).catch((err: any) => {
                this._onVideoError(err.errMsg, err.errCode);
                Debug.log(err.errMsg);
            });
    }

    public initRecord() {

        if (!this.checkWindow()) return;
        var win = this.getWindow();
        let brand = this.getSystemInfoSync().brand.toLowerCase();
        if (/huawei/.test(brand) || /honor/.test(brand)) return;
        // if (!this.isDouyin()) return;
        this.recordObj = win[this.platformName].getVideoRecorderManager();
    }

    /**
     * 开始录屏
     * @param duration 录屏时长
     * @param callback 如果不是抖音回调参数=false
     */
    public startRecord(duration = 120, callback?: (douyin: boolean) => void) {
        Debug.log('record startRecord');
        this.recordRes = null;
        this.recordCb = null;
        if (!this.recordObj) {
            if (callback)
                callback(false);
            return;
        }
        this.recordObj.onStart((res: any) => {
            Debug.log('record onStart');
            if (callback)
                callback(res);
        })

        this.recordObj.onStop((res: any) => {
            this.recordRes = res;
            if (this.recordCb) {
                Debug.log('stop 2');
                this.recordCb(res);
            }
        })

        this.recordObj.start({
            duration
        });
    }
    /**
     * 停止录屏
     * @param callback 如果不是抖音回调参数=false，如果录制成功，回调参数中录屏地址=res.videoPath
     */
    public stopRecord(callback?: (douyin: any) => void) {
        Debug.log('record stopRecord');
        if (!this.recordObj) {
            if (callback)
                callback(false);
            return;
        }
        if (this.recordRes) {
            Debug.log('stop 1');
            if (callback)
                callback(this.recordRes);
        } else {
            this.recordCb = callback;
            this.recordObj.stop();
        }
    }
}